package top.korori.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.korori.common.model.Result;
import top.korori.system.annotation.OptLog;
import top.korori.system.annotation.PreAuth;
import top.korori.system.constant.SysOptTypeConst;
import top.korori.system.entity.SysRole;
import top.korori.system.entity.bo.SysRoleBo;
import top.korori.system.service.SysRoleService;
import top.korori.system.entity.vo.SysRoleVo;

import javax.validation.Valid;
import java.util.List;


/**
 * 
 * 用户角色关联 前端控制器
 * 
 *
 * @author korori
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    public SysRoleService sysRoleService;

    @ApiOperation("获取角色列表")
    @PreAuth("sys:role:list")
    @GetMapping("/list")
    public Result<List<SysRole>> list(){
        return Result.ok(sysRoleService.list());
    }

    @ApiOperation("获取角色选择列表")
    @GetMapping("/getSelect")
    public Result<List<SysRole>> getSelect(){
        List<SysRole> sysRoles = sysRoleService.getNormal();
        return Result.ok(sysRoles);
    }

    @ApiOperation("获取角色")
    @PreAuth("sys:role:query")
    @GetMapping("/get/{roleId}")
    public Result<SysRoleVo> getRole(@PathVariable Long roleId){
        SysRoleVo sysRoleVo = sysRoleService.getRoleMenu(roleId);
        return Result.ok(sysRoleVo);
    }

    @ApiOperation("添加角色")
    @OptLog(optType = SysOptTypeConst.ADD)
    @PreAuth("sys:role:add")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Valid SysRoleBo sysRoleBo){
        sysRoleService.add(sysRoleBo);
        return Result.ok();
    }

    @ApiOperation("修改角色")
    @OptLog(optType = SysOptTypeConst.EDIT)
    @PreAuth("sys:role:edit")
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody @Valid SysRoleBo sysRoleBo){
        sysRoleService.edit(sysRoleBo);
        return Result.ok();
    }

    @ApiOperation("删除角色")
    @OptLog(optType = SysOptTypeConst.DEL)
    @PreAuth("sys:role:del")
    @DeleteMapping("/del/{roleIds}")
    public Result<Void> del(@PathVariable List<Long> roleIds){
        sysRoleService.del(roleIds);
        return Result.ok();
    }


}
